<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>颜色分类</title>
</head>
<body>
  <script>
    function fn(nums) {
      let p = -1
      let q = nums.length
      let i = 0
      while (i < q) {
        if (nums[i] == 0) {
          swap(nums, i, p + 1)
          p = p + 1
          i++
        } else if (nums[i] == 2) {
          swap(nums, i, q - 1)
          q = q - 1
        } else if (nums[i] == 1) {
          i++
        }
      }
      function swap(arr, a, b) {
        let tmp = arr[a];
        arr[a] = arr[b]
        arr[b] = tmp;
      }
      return nums
    }
    console.log(fn([2, 0, 2, 1, 1, 0]))
  </script>
</body>
</html>